package cn.relx.org.controller;

import cn.relx.basic.util.AjaxResult;
import cn.relx.basic.util.PageList;
import cn.relx.org.domain.Employee;
import cn.relx.org.query.EmployeeQuery;
import cn.relx.org.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emp")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /**
     * 添加/修改
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Employee employee) {
        if (null == employee.getId()) {
            // 添加
            employeeService.add(employee);
        } else {
            // 修改
            employeeService.update(employee);
        }
        return AjaxResult.me().success();
    }

    /**
     * 删除 delete localhost:8080/dept/1
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        employeeService.delete(id);
        return AjaxResult.me().success();
    }

    /**
     * 查询所有
     */
    @GetMapping
    public List<Employee> findAll() {
        return employeeService.findAll();
    }

    /**
     * 根据id查询一个
     */
    @GetMapping("/{id}")
    public Employee findById(@PathVariable("id") Long id) {
        return employeeService.findById(id);
    }

    /**
     * 分页查询
     */
    @PostMapping
    public PageList<Employee> queryByPage(@RequestBody EmployeeQuery query) {
        System.out.println(employeeService.queryByPage(query));
        return employeeService.queryByPage(query);
    }

    @PatchMapping
    public AjaxResult batchDel(@RequestBody List<Long> ids) {
        employeeService.batchDel(ids);
        return AjaxResult.me().success();
    }

    @PostMapping("/tree")
    public List<Employee> empDepartmentId() {
        return employeeService.empDepartmentId();
    }

}
